package com.xywy.find.adapter;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.xywy.R;
import com.xywy.common.util.MLog;
import com.xywy.find.bean.Remind;
import com.xywy.me.bean.UserKeeper;

/**
 * 发现 - 提醒
 */
public class RemindAdapter extends BaseAdapter {

	private Context mContext;
	private List<Remind> list;
	private Calendar calendar;
	private DecimalFormat df;
	private SharedPreferences sp;
	private String userid;
	private Drawable drawable;
	private Handler handler;

	public RemindAdapter(Context context,List<Remind> list,Handler handler) {
		this.mContext = context;
		calendar = Calendar.getInstance();
		df =new DecimalFormat("00");
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
		userid = UserKeeper.getCurrentUser(context).getUserid();
		drawable = context.getResources().getDrawable(R.drawable.me_remind_point);
		this.list = list;
		this.handler = handler;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

//	public void setData(List<Remind> stringList) {
//		this.list = stringList;
//	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Hold hold;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.find_remind_list_item, null);

			hold = new Hold();

			hold.hourmin = (TextView) convertView
					.findViewById(R.id.tv_remind_time);
			hold.hintName = (TextView) convertView
					.findViewById(R.id.tv_remind_item_typename);
			hold.repetDay = (TextView) convertView
					.findViewById(R.id.tv_remind_item_repet);

//			hold.rlYmjl = (RelativeLayout) convertView
//					.findViewById(R.id.rl_remind_list_ymjl);
			hold.yaoname = (TextView) convertView
					.findViewById(R.id.tv_remind_yaoname);
			hold.jiliang = (TextView) convertView
					.findViewById(R.id.tv_remind_jiliang);
			hold.tb = (ToggleButton) convertView.findViewById(R.id.tb);
			convertView.setTag(hold);
		} else {
			MLog.i("RemindAdapter", "convertView== not   null  ======");
			hold = (Hold) convertView.getTag();
		}
		final Remind remind = list.get(position);
		hold.hintName.setText(remind.getHint()); // 提醒名称
        
		calendar.setTimeInMillis(remind.getTime());
		hold.hourmin
		.setText((calendar.get(Calendar.HOUR_OF_DAY))+":"+df.format(calendar.get(Calendar.MINUTE)) + "");
		if (remind.getType() == 2) {// 类型 提醒类型（1.每周重复提醒，2.一次性提醒）
			hold.yaoname.setVisibility(View.GONE);
			hold.jiliang.setVisibility(View.GONE);
		} else if (remind.getType() == 1) {
			hold.yaoname.setVisibility(View.VISIBLE);
			hold.jiliang.setVisibility(View.VISIBLE);
		}
		
		int[] dayArray = remind.getLooparray();
		if (null != dayArray && dayArray.length > 0) {
			hold.repetDay.setText(repetDay(dayArray));
		}
		if (remind.getHint().equals(
				mContext.getString(R.string.find_remind_fytx))) {
			String detail = remind.getDetail();
			if (null != detail && detail.length() > 1) {
				String[] de = detail.split(",");
				MLog.i("返回小时  detail :", "返回的detail:=====" + detail);
                if(de.length>1){
                	hold.yaoname.setText(de[0]); // 药名
                	hold.jiliang.setText("剂量 : "+de[1]); // 剂量
                }

//				hold.rlYmjl.setVisibility(View.VISIBLE);
			}
		}
		hold.tb.setOnClickListener(new remindListener());
		hold.tb.setTag(position);
		 drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
		if(remind.isTogglet()){
			hold.tb.setCompoundDrawables(null, null, drawable, null);
			hold.tb.setBackgroundResource(R.drawable.me_remind_normal);
			hold.tb.setChecked(true);
		}else {
			hold.tb.setCompoundDrawables(drawable, null, null, null);
			hold.tb.setBackgroundResource(R.drawable.me_remind_pressed);
			hold.tb.setChecked(false);
		}
		
		convertView.setOnLongClickListener(new OnLongClickListener() {
			

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Message msg = Message.obtain();
				msg.what = 0x1101;
				msg.arg1 = position;
				msg.obj = v;
				handler.sendMessage(msg);
				return false;
			}
		});
		return convertView;
	}

	public class Hold {
//		RelativeLayout rlYmjl;
		TextView hourmin; // 小时分钟
		TextView repetDay; // 重复
		TextView hintName; // 提醒名称
		TextView yaoname; // 药品名称
		TextView jiliang; // 药品剂量
		ToggleButton tb;
	}

	/**
	 * 重复天数
	 */
	public static String repetDay(int[] day) {
		String retStr = "";
		if(day==null){
			return retStr;
		}
		if (day.length == 7) {
			retStr = "每天";
		} else {

			for (int i = 0; i < day.length; i++) {
				int index = day[i];
				String weekStr = "周";
				String weekRet = "";
				switch (index) {
				case 0:
					weekRet = weekStr + "日";
					break;
				case 1:
					weekRet = weekStr + "一";
					break;
				case 2:
					weekRet = weekStr + "二";
					break;
				case 3:
					weekRet = weekStr + "三";
					break;
				case 4:
					weekRet = weekStr + "四";
					break;
				case 5:
					weekRet = weekStr + "五";
					break;
				case 6:
					weekRet = weekStr + "六";
					break;

				}
				retStr += weekRet;
			}
		}
		return retStr;
	}

	private String hourmin(String hourmin) {
		MLog.i("返回小时:",
				"小时" + hourmin.substring(0, 2) + "   分钟："
						+ hourmin.substring(2, 4));
		return hourmin.substring(0, 2) + ":" + hourmin.substring(2, 4);
	}
	
	class remindListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			int p = (Integer) view.getTag();
			   boolean togglet =list.get(p).isTogglet();
			   list.get(p).setTogglet(!togglet);
			   System.out.println(p);
			   notifyDataSetChanged();
		}
	}
	
	
	public List<Remind> getList(){
		return list;
	}
}
